package com.easygoods.controller;

import com.bestvike.stone.spring.controller.BaseController;
import com.bestvike.stone.spring.page.PageableResponse;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.annotation.RequiresPermissions;
import com.easygoods.request.SkuDetailRequest;
import com.easygoods.request.SpuDetailRequest;
import com.easygoods.request.SpuListRequest;
import com.easygoods.request.SpuSearchRequest;
import com.easygoods.request.SpuShareRequest;
import com.easygoods.response.CategoryResponse;
import com.easygoods.response.SkuDetailResponse;
import com.easygoods.response.SpuDetailPublicResponse;
import com.easygoods.response.SpuDetailResponse;
import com.easygoods.response.SpuListResponse;
import com.easygoods.response.SpuMultiShareResponse;
import com.easygoods.response.SpuShareResponse;
import com.easygoods.service.GoodsService;
import com.easygoods.service.HotSpuService;
import com.easygoods.shiro.auth.UserPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-01-02.
 */
@RestController
@RequestMapping(value = "/api/goods")
public class GoodsController extends BaseController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private HotSpuService hotSpuService;

    //获取 category 列表
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/category")
    public CommonResponse<List<CategoryResponse>> getCategoryList() {
        final List<CategoryResponse> goodsCategoryList = this.goodsService.getCategoryList();
        return CommonResponse.success(goodsCategoryList);
    }

    //根据 categoryId 获取 spu 列表
  //  @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/spu/list")
    public CommonResponse<PageableResponse<SpuListResponse>> getSpuList(@RequestBody SpuListRequest request) {
        PageableResponse<SpuListResponse> goodsSpuList = this.goodsService.getSpuList(request);
        return CommonResponse.success(goodsSpuList);
    }

    //根据 关键字 获取 spu 列表
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/spu/search")
    public CommonResponse<PageableResponse<SpuListResponse>> search(@RequestBody SpuSearchRequest request) {
        PageableResponse<SpuListResponse> goodsSpuList = this.goodsService.search(request);
        return CommonResponse.success(goodsSpuList);
    }


    //根据 关键字 获取 spu 列表
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/spu/searchMy")
    public CommonResponse<PageableResponse<SpuListResponse>> searchMy(@RequestBody SpuSearchRequest request) {
        PageableResponse<SpuListResponse> goodsSpuList = this.goodsService.searchMy(request);
        return CommonResponse.success(goodsSpuList);
    }

    //根据 spuId 获取 spu 详情
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/spu/detail")
    public CommonResponse<SpuDetailResponse> getSpuDetail(@RequestBody SpuDetailRequest request) {
        final SpuDetailResponse spuDetail = this.goodsService.getSpuDetail(request);
        return CommonResponse.success(spuDetail);
    }

    //根据 spuId 获取 spu 详情,公开
    @PostMapping("/spu/public")
    public CommonResponse<SpuDetailPublicResponse> getSpuDetailPublic(@RequestBody SpuDetailRequest request) {
        final SpuDetailPublicResponse spuDetail = this.goodsService.getSpuDetailPublic(request);
        return CommonResponse.success(spuDetail);
    }

    //获取 spu 分享信息
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/spu/share")
    public CommonResponse<SpuShareResponse> getSpuShare(@RequestBody SpuShareRequest request) {
        final SpuShareResponse shareDetail = this.goodsService.getSpuShare(request);
        return CommonResponse.success(shareDetail);
    }

    //根据 spuId 和 规格,规格值获取 sku,唯一
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/sku/detail")
    public CommonResponse<SkuDetailResponse> getSkuDetail(@RequestBody SkuDetailRequest request) {
        final SkuDetailResponse skuDetail = this.goodsService.getSkuDetailOnlyOne(request);
        return CommonResponse.success(skuDetail);
    }

    //查询该用户的前五名热销商品
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getHotSpuList")
    public CommonResponse<List<SpuListResponse>> getHotSpuList() {
        final List<SpuListResponse> spuListResponseList = this.hotSpuService.getHotSpuList();
        return CommonResponse.success(spuListResponseList);
    }

    //多个商品分享
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/spu/multiShare")
    public CommonResponse<SpuMultiShareResponse> getSpuMultiShare() {
        final SpuMultiShareResponse spuMultiShare = this.goodsService.getSpuMultiShare();
        return CommonResponse.success(spuMultiShare);
    }
}
